package com.mosai.alignpro.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.mosai.alignpro.util.LogUtils;
import com.mosai.alignpro.R;

/**
 * Created by Rays on 2020/3/13.
 */
public class CameraMaskView extends View {
    public static final String TAG = CameraMaskView.class.getSimpleName();
    private Paint linePaint = new Paint();

    private float cx, cy;
    private Drawable drawable;
    private int iconHeight;
    private float lineMaxY, lineMinY;
    private float iconY;
    private int width;
    private final float lineLength;
    private boolean isDraw;
    private OnBrightnessChangeListener onBrightnessChangeListener;

    public CameraMaskView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

        lineLength = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, displayMetrics);

        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, displayMetrics));
        linePaint.setColor(Color.parseColor("#8BA2F6"));

        drawable = getResources().getDrawable(R.drawable.brightness);
        int iconWidth = drawable.getIntrinsicWidth();
        iconHeight = drawable.getIntrinsicHeight();
        drawable.setBounds(-iconWidth / 2, -iconHeight / 2, iconWidth / 2, iconHeight / 2);
        LogUtils.i(TAG, "drawable.getBounds(): " + drawable.getBounds());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        int height = getMeasuredHeight();
        LogUtils.i(TAG, "onMeasure:"
                + " width: " + width
                + " width: " + MeasureSpec.getSize(widthMeasureSpec)
                + " height: " + height
                + " height: " + MeasureSpec.getSize(heightMeasureSpec)
        );

        lineMinY = (height - lineLength) / 2;
        lineMaxY = (height + lineLength) / 2;
        iconY = height / 2.0f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (!isDraw) {
            return;
        }
        LogUtils.i(TAG, "onDraw:"
                + " width: " + getWidth()
                + " height: " + getHeight()
        );

        float centerX = width / 2.0f;
        float iconMinY = iconY - iconHeight / 2.0f;
        float iconMaxY = iconY + iconHeight / 2.0f - 12;

        if (lineMinY >= iconMinY) {
            canvas.drawLine(centerX, iconMaxY, centerX, lineMaxY, linePaint);
        } else if (lineMaxY <= iconMaxY) {
            canvas.drawLine(centerX, lineMinY, centerX, iconMinY, linePaint);
        } else {
            canvas.drawLine(centerX, lineMinY, centerX, iconMinY, linePaint);
            canvas.drawLine(centerX, iconMaxY, centerX, lineMaxY, linePaint);
        }

        canvas.translate(centerX, iconY);
        drawable.draw(canvas);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float dx = event.getX() - cx;
        float dy = event.getY() - cy;
        cx = event.getX();
        cy = event.getY();

        int action = event.getAction();
        LogUtils.i(TAG, "onTouchEvent:"
                + " action: " + action
                + " cx: " + cx
                + " cy: " + cy
                + " dx: " + dx
                + " dy: " + dy
        );

        switch (action) {
            case MotionEvent.ACTION_MOVE:
                float y = iconY + dy / 5;
                if (y < lineMinY) {
                    iconY = lineMinY;
                } else if (y > lineMaxY) {
                    iconY = lineMaxY;
                } else {
                    iconY = y;
                }
                invalidate();
                onBrightnessChange();
                break;
            case MotionEvent.ACTION_DOWN:
                isDraw = true;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                isDraw = false;
                invalidate();
                break;
        }
        return true;
    }

    private void onBrightnessChange() {
        if (onBrightnessChangeListener != null) {
            float rate = (lineMaxY - iconY) / (lineMaxY - lineMinY);
            LogUtils.i(TAG, "onBrightnessChange:"
                    + " rate: " + rate
            );
            onBrightnessChangeListener.onBrightnessChange(rate);
        }
    }

    public void setOnBrightnessChangeListener(OnBrightnessChangeListener onBrightnessChangeListener) {
        this.onBrightnessChangeListener = onBrightnessChangeListener;
    }

    public interface OnBrightnessChangeListener {
        void onBrightnessChange(float rate);
    }

}
